import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getEchartData } from '@/axios/api/picking_team/eu_rework'

const useEuStore = defineStore('eu', () => {

    let xData = ref<any>([])
    let yData = ref<any>([])

    // 定义一个比较函数
    // function dateCompare(a: string, b: string): number {
    //     return new Date(a).getTime() - new Date(b).getTime(); // 使用 Date 对象转换字符串日期并进行比较
    // }


    // 获取数据
    async function getData() {
        xData.value = [];
        yData.value = [];
        try {
            let res: any = await getEchartData();
            let data = JSON.parse(res.data);
            const summary: any = {};
            data.forEach((item: any) => {
                const date = item.NRC_Time.split(' ')[0]; // 提取日期部分
                if (!summary[date]) {
                    summary[date] = 0; // 初始化日期对应的数量为0
                }
                summary[date] += item.Qty; // 将相同日期的数量相加
            });
            data.sort((a:any, b:any) => {
                const dateA:any = new Date(a.NRC_Time);
                const dateB:any = new Date(b.NRC_Time);
                return dateA - dateB;
            });
            xData.value = data.map((item: any) => item.NRC_Time.split(' ')[0]);
            yData.value = data.map((item: any) => item.Qty);
            return 'ok';
        } catch (error: any) {
            return Promise.reject(new Error(error));
        }
    }

    return {
        xData,
        yData,
        getData
    }
})

export default useEuStore